Imports DotNetNuke.UI.WebControls
Imports System.IO

Partial Class DNNTree
	Inherits System.Web.UI.Page

	Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		'Put user code to initialize the page here

		SetTreeDefaults()
	End Sub

	Private Sub SetTreeDefaults()

		'Me.chkPopulateFromClient.Enabled = Not Me.chkForceDownLevel.Checked
		'If Me.chkPopulateFromClient.Enabled = False Then Me.chkPopulateFromClient.Checked = False
		Me.MyDNNTree.PopulateNodesFromClient = Me.chkPopulateFromClient.Checked
		Me.MyDNNTree.ForceDownLevel = chkForceDownLevel.Checked
        Me.MyDNNTree.CheckBoxes = chkCheckboxes.Checked
        Me.MyDNNTree.CheckBoxMode = CType(ddlCheckBoxMode.SelectedValue, eCheckBoxMode)
		Me.MyDNNTree.JSFunction = "myTestFunction('TestParam1', 'TestParam2');"
		Me.MyDNNTree.Target = "MyTarget"
		Me.MyDNNTree.IsCrawler = chkIsCrawler.Checked
		If IsNumeric(Me.txtAnimationFrames.Text) Then Me.MyDNNTree.AnimationFrames = CInt(Me.txtAnimationFrames.Text)
		Me.txtDir.Text = Request.MapPath("")
		If IsNumeric(txtIndent.Text) Then
			MyDNNTree.IndentWidth = CInt(txtIndent.Text)
		Else
			txtIndent.Text = "20"
		End If
		If Not IsPostBack Then

			MyDNNTree.ImageList.Add("images/folder.gif")
			MyDNNTree.ImageList.Add("images/file.gif")

			'MyDNNTree.UseClientEvents = True
			PopulateTree(Me.txtDir.Text)
		ElseIf Me.MyDNNTree.TreeNodes.Count > 0 AndAlso Me.txtDir.Text.ToLower <> Me.MyDNNTree.TreeNodes(0).Key.ToLower Then
			PopulateTree(Me.txtDir.Text)
		End If

		If Me.IsPostBack Then
			Me.txtStatus.Value = "Postback occurred: " & Now.ToString & vbCrLf & Me.txtStatus.Value
		End If

	End Sub

	Private Sub PopulateTree(ByVal strPath As String)
		MyDNNTree.TreeNodes.Clear()

		Dim objNode As TreeNode = New TreeNode("root")
		objNode.Key = strPath
		objNode.ToolTip = objNode.Text
		objNode.ImageIndex = eImageType.Folder
		objNode.ClickAction = eClickAction.Expand
		objNode.HasNodes = True
		'objNode.PopulateOnDemand = True
		MyDNNTree.TreeNodes.Add(objNode)
		If Me.MyDNNTree.PopulateNodesFromClient = False OrElse Me.MyDNNTree.IsCrawler = True Then PopulateChildrenTreeNodes(objNode)

	End Sub

	Private Sub PopulateChildrenTreeNodes(ByVal objParent As TreeNode)
		Dim strKey As String = objParent.Key
		If strKey.EndsWith("\") = False Then strKey &= "\"
		If strKey.IndexOf(Request.MapPath("")) = -1 Then
			Return
		End If
		Dim files As String() = Directory.GetFileSystemEntries(strKey)
		Dim index As Integer = 0
		While index < files.Length
			Dim objFileInfo As FileInfo = New FileInfo(files(index))
			Dim objTreeNode As TreeNode
			Dim intIndex As Integer
			If objParent Is Nothing Then
				intIndex = MyDNNTree.TreeNodes.Add()
				objTreeNode = MyDNNTree.TreeNodes(intIndex)
			Else
				intIndex = objParent.TreeNodes.Add()				 'New TreeNode(objParent.XmlNode, MyDNNTree)
				objTreeNode = objParent.TreeNodes(intIndex)
			End If
            objTreeNode.Text = objFileInfo.Name
			objTreeNode.Key = objFileInfo.FullName
			objTreeNode.ToolTip = objFileInfo.Name
			'objTreeNode.PopulateOnDemand = True

			If objTreeNode.Key.EndsWith(".js") Then
				objTreeNode.JSFunction = "myJSExtFunction('" & objTreeNode.Key & "');"
				objTreeNode.CssClass = "SpecialNode"
				objTreeNode.CssClassOver = "SpecialNodeHover"
			End If
			'if file is htm and contained is relative to the currently browsed page then allow navigation
			If (objTreeNode.Key.EndsWith(".htm") OrElse objTreeNode.Key.EndsWith(".xml")) AndAlso objTreeNode.Key.IndexOf(Server.MapPath(Request.ApplicationPath)) > -1 Then
				Dim sNav As String = objTreeNode.Key.Replace(Server.MapPath(Request.ApplicationPath & "\"), "").Replace("\", "/")
				objTreeNode.JSFunction = "myJSNavFunction('" & sNav & "');"
				objTreeNode.ClickAction = eClickAction.Navigate
				objTreeNode.NavigateUrl = sNav				'& "?a=1&b=2"
			End If

			If objTreeNode.Key.EndsWith(".vb") Then
				objTreeNode.ClickAction = eClickAction.None
				objTreeNode.CssClass = "DisabledNode"
				objTreeNode.Enabled = False
			End If
            If (File.GetAttributes(files(index)) And FileAttributes.Directory) = FileAttributes.Directory AndAlso objTreeNode.Level < 3 Then
                objTreeNode.ImageIndex = eImageType.Folder
                'if we are not populating from client we need to populate all the treenodes now
                If Me.MyDNNTree.PopulateNodesFromClient = False OrElse Me.MyDNNTree.IsCrawler = True Then PopulateChildrenTreeNodes(objTreeNode)
                Try
                    Dim objDir As DirectoryInfo = New DirectoryInfo(objFileInfo.FullName)
                    objTreeNode.HasNodes = objDir.GetFiles.Length > 0 OrElse objDir.GetDirectories.Length > 0
                    objTreeNode.ClickAction = eClickAction.Expand
                Catch ex As Exception
                End Try

            Else
                'objTreeNode.JSFunction = "myTestFunction('a', 1);"
                objTreeNode.ImageIndex = eImageType.Page
            End If
			index += 1
		End While
	End Sub

    Private Sub MyDNNTree_PopulateOnDemand(ByVal source As Object, ByVal e As DotNetNuke.UI.WebControls.DNNTreeEventArgs) Handles MyDNNTree.PopulateOnDemand
        PopulateChildrenTreeNodes(e.Node)
    End Sub

	Public Enum eImageType
		Folder
		Page
	End Enum

	Private Sub MyDNNTree_NodeClick(ByVal source As Object, ByVal e As DNNTreeNodeClickEventArgs) Handles MyDNNTree.NodeClick
		Dim nodeInfo As String
		nodeInfo &= "<b>Node ID:  </b>"
		nodeInfo &= e.Node.ID & "<br>"
		nodeInfo &= "<b>Node Text:  </b>"
		nodeInfo &= e.Node.Text & "<br>"
		nodeInfo &= "<b>Node Tooltip:  </b>"
		nodeInfo &= e.Node.ToolTip & "<br>"
		nodeInfo &= "<b>Node HasChildren:  </b>"
		nodeInfo &= e.Node.HasNodes.ToString
		If e.Node.HasNodes Then
			nodeInfo &= "<br><b>Node Expanded:  </b>"
			nodeInfo &= e.Node.IsExpanded.ToString
		End If
		nodeInfo &= "<br>"
		If MyDNNTree.CheckBoxes Then
			Dim objNode As TreeNode
			For Each objNode In MyDNNTree.SelectedTreeNodes()
				nodeInfo &= "<b>Selected Node Text:  </b>" & objNode.Text & "<br>"
			Next
		End If

		lblEventDetails.Text = nodeInfo
	End Sub

	Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender

	End Sub

	Private Sub chkCheckboxes_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkCheckboxes.CheckedChanged
	End Sub


	Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
		MyBase.OnInit(e)
		DotNetNuke.UI.Utilities.ClientAPI.HandleClientAPICallbackEvent(Me)
	End Sub

	Private Sub chkPopulateFromClient_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkPopulateFromClient.CheckedChanged
		PopulateTree(Me.txtDir.Text)
	End Sub

	Private Sub chkForceDownLevel_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkForceDownLevel.CheckedChanged
		PopulateTree(Me.txtDir.Text)
	End Sub

	Private Sub chkIsCrawler_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkIsCrawler.CheckedChanged
		PopulateTree(Me.txtDir.Text)
	End Sub

End Class
